package cc.io24.knewcafe.boss.domain;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.*;

@Entity
@EntityListeners(AuditingEntityListener.class)
@SQLDelete(sql = "update sys_user set is_deleted = 1 where id = ?")
@Where(clause = "is_deleted = 0")
@org.hibernate.annotations  .Table(appliesTo = "sys_user",comment="系统用户表")
public class SysUser implements Serializable {

    @Id
    @GeneratedValue(generator = "uuid4i")
    @GenericGenerator(name = "uuid4i", strategy = "uuid2")
    @Column(name="id",columnDefinition = "varchar(64) not null comment 'id'")
    private String id ;

    @Column(name="username",columnDefinition = "varchar(64) not null comment '登录名'")
    private String username;

    @Column(name="password",columnDefinition = "varchar(64) not null comment '密码'")
    private String password;

    @Column(name="nickname",columnDefinition = "varchar(64) not null comment '昵称'")
    private String nickname;

    @Column(name="email",columnDefinition = "varchar(64) not null comment 'Email'")
    private String email;

    @ManyToMany(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
    @JoinTable(name="sys_user_role",
            joinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")}
    )
    private Set<SysRole> roles;

    @Transient
    private Set<String> role_ids;

    @Column(name = "is_deleted")
    private Integer isDeleted = 0;

    @CreatedDate
    @Column(name = "create_date")
    private Date createDate;

    @LastModifiedDate
    @Column(name = "lastModified_date")
    private Date lastModifiedDate;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Set<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(Set<SysRole> roles) {
        this.roles = roles;
    }

    public Set<String> getRole_ids() {
        if(roles!=null&&roles.size()!=0){
            if(role_ids==null){
                role_ids = new HashSet<>();
            }
            for(SysRole sysRole:roles){
                role_ids.add(sysRole.getId());
            }
        }
        return role_ids;
    }

    public void setRole_ids(Set<String> role_ids) {
        if(role_ids!=null){
            roles.clear();
            for (String id :role_ids ) {
                SysRole role = new SysRole();
                role.setId(id);
                roles.add(role);
            }
            this.role_ids = role_ids;
        }
    }

    public Integer getIsDeleted() {
        return isDeleted;
    }

    public void setIsDeleted(Integer isDeleted) {
        this.isDeleted = isDeleted;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(Date lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }
}
